package com.cr.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.cr.model.Customer;
import com.cr.util.DBUtil;

/**
 * @author Administrator
 *
 */
public class CustomerDao {
	private DBUtil db=new DBUtil();
	
	/**
	 * @param id
	 * @return
	 */
	public Customer getCustomer(long id)
	{
		Customer customer=null;
		
		String sql="select * from customer where Id=?";
		Object[] args={id};
		ResultSet rs=db.preQuery(sql, args);
		
		if(rs!=null)
		{
			try {
				if(rs.next())
				{
					customer=new Customer();
					
					customer.setAddress(rs.getString("Address"));
					customer.setAge(rs.getInt("Age"));
					customer.setId(id);
					customer.setName(rs.getString("Name"));
					customer.setPassword(rs.getString("Password"));
					customer.setPhone(rs.getString("Phone"));
					customer.setRemark(rs.getString("Remark"));
					customer.setSex(rs.getString("Sex"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return customer;
	}
	/**
	 * @param name
	 * @param password
	 * @return
	 */
	public Customer getCustomer(String name,String password)
	{
		Customer customer=null;
		
		String sql="select * from customer where Name=? and Password=?";
		Object[] args={name,password};
		ResultSet rs=db.preQuery(sql, args);
		
		if(rs!=null)
		{
			try {
				if(rs.next())
				{
					customer=new Customer();
					
					customer.setAddress(rs.getString("Address"));
					customer.setAge(rs.getInt("Age"));
					customer.setId(rs.getLong("Id"));
					customer.setName(name);
					customer.setPassword(password);
					customer.setPhone(rs.getString("Phone"));
					customer.setRemark(rs.getString("Remark"));
					customer.setSex(rs.getString("Sex"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return customer;
	}
	/**
	 * @param customer
	 * @return
	 */
	public boolean addCustomer(Customer customer)
	{
		boolean flag=false;
		String sql="insert into customer(Address,Age,Name,Password,Phone,Remark,Sex) ";
		sql+=" values(?,?,?,?,?,?,?)";
		
		Object[] args={customer.getAddress(),
				customer.getAge(),
				customer.getName(),
				customer.getPassword(),
				customer.getPhone(),
				customer.getRemark(),
				customer.getSex()};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * @param customer
	 * @return
	 */
	public boolean updateCustomer(Customer customer)
	{
		boolean flag=false;
		String sql="update customer set ";
		sql+=" Address=?,";
		sql+=" Age=?,";
		sql+=" Name=?,";
		sql+=" Password=?,";
		sql+=" Phone=?,";
		sql+=" Remark=?,";
		sql+=" Sex=?";
		sql+=" where Id=?";
		
		Object[] args={customer.getAddress(),
				customer.getAge(),
				customer.getName(),
				customer.getPassword(),
				customer.getPhone(),
				customer.getRemark(),
				customer.getSex(),
				customer.getId()};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * @param id
	 * @return
	 */
	public boolean deleteCustomer(long id)
	{
		boolean flag=false;
		String sql="delete from customer where Id=?";
		
		Object[] args={id};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
}
